<template>
  <el-container>
    <el-header>
      <div class="header-content">
        <OrderHeader />
      </div>
    </el-header>

    <el-main>
      <el-row :gutter="20">
        <!-- 循环渲染订单数据 -->
        <el-col :span="6" v-for="order in orders" :key="order.orderId">
          <el-card shadow="hover">
            <div class="card-content">
              <div class="left-logo">
                <div class="urge">
                  <el-icon color="red" v-if="order.urgency === '是'"><Bell /></el-icon>
                  <el-icon color="red" v-else></el-icon>
                </div>
                <div class="fly-logo">
                  <img
                    src="https://cityexpress.yellowswimming.cn/img/if-pigeon-1%20Copy%204.svg"
                    alt="描述"
                  />
                </div>
              </div>
              <div class="concrete">
                <span>{{ order.productType }}</span>
                <div>预计时间： 1h</div>
                <div>目的地： {{ order.address }}</div>
                <div>距离： 2km</div>
                <div>预计收益： 1w</div>
                <div>
                  <el-button :icon="Search" @click="viewDetails(order)"
                    >查看详情</el-button
                  >
                  <el-button :disabled="order.deliveryStatus !== '未派单'" :type="order.deliveryStatus === '未派单' ? 'default' : 'success'" :icon="Check" @click="grabOrder(order,order.orderId,courierName)"
                    >抢单</el-button
                  >
                </div>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </el-main>
  </el-container>
</template>

<script setup lang="ts">
import { onMounted, ref } from "vue";
import axios from "axios";
import { useRouter } from 'vue-router';
import OrderHeader from "@/components/OrderHeader.vue";
import { Check, Search, Bell } from "@element-plus/icons-vue";
import {useAuthStore} from "@/store/userInfo";

const userAuth = useAuthStore();

const courierName = ref<string>(userAuth.username);
interface Order {
  orderId: number;
  senderId: string;
  expressMethod: string;
  deliveryStatus: string;
  orderTime: string;
  trackingNumber: string;
  wareHouse: string;
  returnStatus: string;
  payTime: string;
  buyWay: string;
  payWay: string;
  productType: string;
  address: string;
  moreInformation: string;
  urgency: string;
}
const orders = ref<Order[]>([]);

const router = useRouter();
// 一旦组件被挂载，立即请求订单数据
onMounted(async () => {
  try {
    const response = await axios.get("http://localhost:8085/order/allOrders");
    orders.value = response.data.map((order: any) => ({
      orderId: order.orderId,
      senderId: order.senderId,
      expressMethod: order.expressMethod,
      deliveryStatus: order.deliveryStatus,
      orderTime: order.orderTime,
      trackingNumber: order.trackingNumber,
      wareHouse: order.wareHouse,
      returnStatus: order.returnStatus,
      payTime: order.payTime,
      buyWay: order.buyWay,
      payWay: order.payWay,
      productType: order.productType,
      address: order.address,
      moreInformation: order.moreInformation,
      urgency: order.urgency,
    }));
  } catch (error) {
    console.error("There was an error fetching the orders:", error);
    // 这里处理错误情况，例如显示通知或消息
  }
});

// 抢单操作的方法
const grabOrder = async (order:Order,orderId: number, name: string) => {
  console.log("抢单操作", orderId);

  try {
    
    const response = await axios.post(`http://localhost:8085/order/grab/${orderId}`, {
      courierName: name, // 假设后端需要快递员名字作为请求体的一部分
    });
    order.deliveryStatus = '正在拣货'
    console.log("抢单成功", response.data);
  } catch (error:any) {
    // 请求失败，处理错误
    if (axios.isAxiosError(error)) {
      // 如果错误是 AxiosError，可以获取到更多关于请求失败的信息
      console.error("抢单失败", error.response?.data);
    } else {
      // 其他类型的错误
      console.error("Error", error.message);
    }
  }
};
const viewDetails = (order:Order) => {
  router.push({
    name: 'orderDetail',
    query: { 
      orderId: order.orderId,
      senderId: order.senderId,
      expressMethod: order.expressMethod,
      deliveryStatus: order.deliveryStatus,
      orderTime: order.orderTime,
      trackingNumber: order.trackingNumber,
      wareHouse: order.wareHouse,
      returnStatus: order.returnStatus,
      payTime: order.payTime,
      buyWay: order.buyWay,
      payWay: order.payWay,
      productType: order.productType,
      address: order.address,
      moreInformation: order.moreInformation,
      urgency: order.urgency
    }
  });
};
</script>

<style scoped lang="less">
.el-container {
  background: #f5f5f5;
  border-radius: 8px;
  overflow: hidden;
}
.el-card {
  margin-bottom: 10px;
}
.el-header {
  color: rgb(255, 255, 255);

  font-size: 24px;
  text-align: center;
  .header-content {
    height: 100%;
  }
}

.card-content {
  display: flex;
  justify-content: center;
  align-items: center;

  .left-logo {
    flex: 1;

    display: flex;
    flex-direction: column;
    .urge {
      height: 30px;

      display: flex;
      align-items: center;
      justify-content: center;
    }
    .fly-logo {
      height: 100px;

      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
  .concrete {
    flex: 3;
    margin-left: 10px;
    span {
      font-size: 1rem;
      font-weight: 700;
    }
    div {
      margin-top: 0.3rem;
      font-size: 0.8rem;
      font-weight: 500;
    }
    .el-button {
      font-size: 0.8rem;
      margin-top: 0.2rem;
      margin-right: 2px;
    }
  }
}
</style>
